package com.kc.business.service.impl;

import com.kc.business.domain.vo.KcUserStatisticsVO;
import com.kc.business.mapper.BizUserStatisticsMapper;
import com.kc.business.service.IBizUserStatisticsService;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @ClassName BizUserStatisticsServiceImpl
 * @Description 用户统计service实现
 * @Author wangdongliang
 * @Date 2021/5/17 10:45
 */
@Service
public class BizUserStatisticsServiceImpl implements IBizUserStatisticsService {
    @Resource
    private BizUserStatisticsMapper userStatisticsMapper;
    @Override
    public List<KcUserStatisticsVO> getNumByArea() {
        return userStatisticsMapper.getNumByArea();
    }

    @Override
    public List<KcUserStatisticsVO> getNumByLevel() {
        List<KcUserStatisticsVO> userStatisticsVOS = userStatisticsMapper.getNumByLevel();
        List<KcUserStatisticsVO> statisticsVOS = new ArrayList<>();
        //将查询到的列表数据转成map
        Map<String,String> map = new HashMap<>();
        userStatisticsVOS.forEach(userStatisticsVO->{
            map.put(userStatisticsVO.getName(),userStatisticsVO.getValue());
        });
        //处理等级
        for(int i = 1;i<=12;i++){
            String key = i+"";
            KcUserStatisticsVO kcUserStatisticsVO = new KcUserStatisticsVO();
            kcUserStatisticsVO.setName(i+"级");
            if(map.get(key) == null){
                //未查到
                kcUserStatisticsVO.setValue("0");
            }else{
                kcUserStatisticsVO.setValue(map.get(key));
            }
            statisticsVOS.add(kcUserStatisticsVO);
        }
        return statisticsVOS;
    }

    @Override
    public Integer getNumByAuthStatus(Integer realStatus, Integer faceStatus) {
        Integer num = userStatisticsMapper.getNumByAuthStatus(realStatus,faceStatus);
        return num == null ? 0 : num;
    }

    @Override
    public Integer getTotalNum() {
        return userStatisticsMapper.getTotalNum();
    }

    @Override
    public Integer getNumByLowerLevel(Integer havaLowerLevelStstus) {
        Integer num = userStatisticsMapper.getNumByLowerLevel(havaLowerLevelStstus);
        return num == null ? 0 : num;
    }

    @Override
    public List<KcUserStatisticsVO> getNumByAgeSection() {
        List<KcUserStatisticsVO> kcUserStatisticsVOS = userStatisticsMapper.getNumByAgeSection();
        Map<String,String> map = new HashMap<>();
        kcUserStatisticsVOS.forEach(kcUserStatisticsVO -> {
            map.put(kcUserStatisticsVO.getName(),kcUserStatisticsVO.getValue());
        });
        //获取用户总数
        Integer totalNum = userStatisticsMapper.getAuthNum();
        BigDecimal total = new BigDecimal(totalNum).divide(new BigDecimal(100));
        //需要统计得数据
        List<String> ageSection = new ArrayList<>();
        ageSection.add("18岁以下");
        ageSection.add("18-22岁");
        ageSection.add("23-30岁");
        ageSection.add("31-35岁");
        ageSection.add("36-40岁");
        ageSection.add("41-50岁");
        ageSection.add("51岁以上");
        List<KcUserStatisticsVO> statisticsVOS = new ArrayList<>();
        for (String s : ageSection){
            KcUserStatisticsVO kcUserStatisticsVO = new KcUserStatisticsVO();
            kcUserStatisticsVO.setName(s);
            if(map.get(s) != null){
                BigDecimal num = new BigDecimal(map.get(s));
                kcUserStatisticsVO.setValue(num.divide(total,2,BigDecimal.ROUND_FLOOR).toString());
            }else{
                kcUserStatisticsVO.setValue("0");
            }
            statisticsVOS.add(kcUserStatisticsVO);
        }
        return statisticsVOS;
    }

    @Override
    public List<KcUserStatisticsVO> getNumBySex() {
        List<KcUserStatisticsVO> kcUserStatisticsVOS = userStatisticsMapper.getNumBySex();
        kcUserStatisticsVOS.forEach(kcUserStatisticsVO -> {
            if(kcUserStatisticsVO.getName().equals("1.0")){
                kcUserStatisticsVO.setName("男");
            }else{
                kcUserStatisticsVO.setName("女");
            }
        });
        return kcUserStatisticsVOS;
    }

    @Override
    public List<KcUserStatisticsVO> getNumByAreaRatio() {
        //用户总数
        Integer totalNum = userStatisticsMapper.getTotalNum();
        BigDecimal total = new BigDecimal(totalNum).divide(new BigDecimal(100));
        List<KcUserStatisticsVO> kcUserStatisticsVOS = userStatisticsMapper.getNumByArea();
        kcUserStatisticsVOS.forEach(kcUserStatisticsVO -> {
            BigDecimal num = new BigDecimal(kcUserStatisticsVO.getValue());
            kcUserStatisticsVO.setValue(num.divide(total,2,BigDecimal.ROUND_FLOOR).toString());
        });
        return kcUserStatisticsVOS;
    }

    @Override
    public List<String> getAgeAection() {
        List<String> ageSection = new ArrayList<>();
        ageSection.add("18岁以下");
        ageSection.add("18-22岁");
        ageSection.add("23-30岁");
        ageSection.add("31-35岁");
        ageSection.add("36-40岁");
        ageSection.add("41-50岁");
        ageSection.add("51岁以上");
        return ageSection;
    }
}
